有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

SpringBootApplication没有获取java@Service注释bean

我在一个小项目中使用了SpringBoot应用程序。架构很简单:

输入文件:

@SpringBootApplication
@PropertySource("classpath:application.properties")
public class RptApp implements CommandLineRunner 
{ 
  @Autowired private RptService rptService; 
  @Override
  public void run(String... args) throws Exception {
    rptService.doStuff(){...};
  }
}

其中RptService是一个接口,它有一个实现:RptServiceImpl。JAVA这是RptServiceImpl。java被注释为@Service

@Service
public class RptServiceImpl implements RptService {
  @Override
  public void doStuff();
}

我的理解是@SpringBootApplication已经嵌入了@ComponentScan、@EnableComponentScan(或类似的东西)、@配置,所以rptService应该由容器自动连接。相反,它抛出了一个错误,如:

Description:

Field RptService in XXXX.RptApp required a bean of type 'xxx.xxx.xxx.RptService' that could not be found.


Action:

Consider defining a bean of type 'xxx.xxx.xxx.RptService' in your configuration.

我知道如何根据提示找到解决方法,但除了要点之外

我确实编写了另一个简单的类客户机,并在主文件中用@Component和@Autowired对其进行注释。斯普林把它捡起来没问题

我的pom文件的相关部分如下所示:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>1.4.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
   <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
....

有人能帮我点忙吗


共 (1) 个答案

  1. # 1 楼答案

    RptService应该在要拾取的主条目文件的子目录中@ComponentScan